package twitter.functionalities;

import java.util.ArrayList;
import java.util.List;

import twitter4j.DirectMessage;
import twitter4j.IDs;
import twitter4j.Paging;
import twitter4j.Twitter;
import twitter4j.TwitterException;

public class MessageSender {

	private Twitter twitter;
	private List<Long> newFollowers = new ArrayList<Long>();
	private final static String MESSAGE = "Ciao, con la nostra app Android puoi trovare i concerti vicino a te e segnalare quelli della tua band! https://play.google.com/store/apps/details?id=it.rockliveitalia&referrer=utm_source%3Dtwitter%26utm_medium%3Dsocial";

	public MessageSender(Twitter twitter) {
		this.twitter = twitter;
	}

	public void sendMessages(String myName, String startingName)
			throws TwitterException {

		long cursor = -1;
		IDs list;
		long[] u = new long[1];

		List<DirectMessage> messageList = new ArrayList<DirectMessage>();

		Paging paging = new Paging(1, 200);
		messageList.addAll(twitter.getSentDirectMessages(paging));

		long lastID = 0;
		int errors = 0;

		boolean found = false;
		for (int i = 0; i < messageList.size() && !found; i++) {
			boolean secondMessage = false;
			for (int j = i + 1; j < messageList.size() && !found
					&& !secondMessage; j++) {
				if (messageList.get(i).getRecipientId() == messageList.get(j)
						.getRecipientId())
					secondMessage = true;
			}
			if (!secondMessage) {
				found = true;
				u[0] = messageList.get(i).getRecipientId();
				try {
					twitter.lookupUsers(u).get(0).getScreenName();
					lastID = messageList.get(i).getRecipientId();
				} catch (Exception e) {
					errors++;
					found = false;
					e.printStackTrace();
					lastID = 0;
				}
			}
		}
		if (errors <= 3) {
			boolean alreadySent = false;

			do {
				list = twitter.getFollowersIDs(myName, cursor);
				for (int i = 0; i < list.getIDs().length && !alreadySent; i++) {
					if (list.getIDs()[i] == lastID) {
						alreadySent = true;
					} else {
						newFollowers.add(list.getIDs()[i]);
					}
				}
				cursor = list.getNextCursor();
			} while (cursor != 0 && !alreadySent);

			// u[0] = lastID;
			// System.out.println(twitter.lookupUsers(u).get(0).getScreenName());

			boolean limitAPIreached = false;
			if (lastID != 0)
				for (int i = newFollowers.size() - 1; i >= 0
						&& !limitAPIreached; i--) {

					try {
						// u[0] = newFollowers.get(i);
						// System.out.println(twitter.lookupUsers(u).get(0)
						// .getScreenName());
						twitter.sendDirectMessage(newFollowers.get(i), MESSAGE);
					} catch (TwitterException e) {
						e.printStackTrace();
						limitAPIreached = true;
						u[0] = newFollowers.get(i);

					}
				}
			if (limitAPIreached)
				System.out.println(twitter.lookupUsers(u).get(0)
						.getScreenName());

		}
	}

	public void sendMessageTest() {
		try {
			twitter.sendDirectMessage("naedius", MESSAGE);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
